<?php
/**
 * Core_Acl_Resource_Action
 * 
 * @author naspeh
 * @version $Id: Action.php 658 2008-06-13 14:24:51Z naspeh $
 */
class Core_Acl_Resource_Action extends Core_Acl_Resource_Abstract 
{
    /**
     * Связное поле в таблице ресурсов
     *
     * @var string
     */
    protected $_field = 'action_id';
    /**
     * Активирует ресурс в объекте Acl
     *
     * @param Core_Acl $acl
     * @return Core_Acl
     */    
    public function active(Core_Acl & $acl)
    {
        $result = array();
        $resource = new Core_Acl_Resource_Action($this->_row);
        while (!is_null($resource->parent)) {
            $tableActions = new Core_Table_Actions();    
            $row = $tableActions->fetchRow(array('id = ?' => $resource->parent))->toArray();
            $parent = new Core_Acl_Resource_Action($row);
            $result[] = array('resource'=>$resource, 'parent'=>$parent->getResourceId());
            $resource = $parent;
        }
        $result[]  = array('resource'=>$resource, 'parent'=>null);
        $result = array_reverse($result);
        foreach ($result as $item) {
            if (!$acl->has($item['resource'])) {
                if (is_null($item['parent'])) {
                    $acl->active($this->getResourceType());
                	$acl->add($item['resource'], $this->getResourceType());
                } else {
                    $acl->add($item['resource'], $item['parent']);
                }
            }
        }
        return $acl;
    }    
}